<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="costcenterList"
      row-key="number" border stripe
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="编码" prop="number" />
      <el-table-column label="名称" align="center" prop="name" />
<!--      <el-table-column label="上级编码" align="center" prop="parentnumber" />-->
      <el-table-column label="类型" align="center" prop="costtype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bom_costcentertype" :value="scope.row.costtype"/>
        </template>
      </el-table-column>
      <el-table-column label="是否封存" align="center" prop="issealup">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.issealup"/>
        </template>
      </el-table-column>
      <el-table-column label="是否业务实体" align="center" prop="isbizunit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.isbizunit"/>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改成本中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="number">
          <el-input v-model="form.number" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="costtype">
          <el-select v-model="form.costtype" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.bom_costcentertype"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否封存" prop="issealup">
          <el-select v-model="form.issealup" placeholder="请选择是否封存">
            <el-option
              v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否业务实体" prop="isbizunit">
          <el-select v-model="form.isbizunit" placeholder="请选择是否业务实体">
            <el-option
              v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上级编码" prop="parentnumber">
          <treeselect v-model="form.parentnumber" :options="costcenterOptions" :normalizer="normalizer" placeholder="请选择上级编码" />
        </el-form-item>
        <el-form-item label="委托财务组织编码" prop="comnumber">
          <el-input v-model="form.comnumber" placeholder="请输入委托财务组织编码" />
        </el-form-item>
        <el-form-item label="委托财务组织名称" prop="comname">
          <el-input v-model="form.comname" placeholder="请输入委托财务组织名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCostcenter, getCostcenter, delCostcenter, addCostcenter, updateCostcenter } from "@/api/bomcostestimation/basedocument/costcenter";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Costcenter",
  dicts: ['yes_no', 'bom_costcentertype'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 成本中心表格数据
      costcenterList: [],
      // 成本中心树选项
      costcenterOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        number: null,
        name: null,
        costtype: null,
        issealup: null,
        isbizunit: null,
        parentnumber: null,
        parentname: null,
        comnumber: null,
        comname: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询成本中心列表 */
    getList() {
      this.loading = true;
      listCostcenter(this.queryParams).then(response => {
        this.costcenterList = this.handleTree(response.data, "number", "parentnumber");
        this.loading = false;
      });
    },
    /** 转换成本中心数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.number,
        label: node.name,
        children: node.children
      };
    },
	/** 查询成本中心下拉树结构 */
    getTreeselect() {
      listCostcenter().then(response => {
        this.costcenterOptions = [];
        const data = { number: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "number", "parentnumber");
        this.costcenterOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        name: null,
        costtype: null,
        issealup: null,
        isbizunit: null,
        parentnumber: null,
        parentname: null,
        comnumber: null,
        comname: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.number) {
        this.form.parentnumber = row.number;
      } else {
        this.form.parentnumber = 0;
      }
      this.open = true;
      this.title = "添加成本中心";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentnumber = row.number;
      }
      getCostcenter(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成本中心";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCostcenter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCostcenter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除成本中心编号为"' + row.id + '"的数据项？').then(function() {
        return delCostcenter(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
